package com.wantup;

import com.wantup.ext_proxy.$Proxy01;
import com.wantup.ext_proxy.MyExtInvocationHandler;
import com.wantup.service.OrderService;
import com.wantup.service.cglib.CglibMethodInterceptor;
import com.wantup.service.impl.OrderServiceImpl;
import com.wantup.service.proxy.JdkInvocationHandler;
import com.wantup.service.proxy.OrderServiceProxy;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;

public class ProxyDemoMain {

    public static void main(String[] args) throws Throwable {
        //1. 静态代理
//        OrderService orderService = new OrderServiceImpl();
//        new OrderServiceProxy(orderService).order();

        //2.动态代理(java)
//        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
//        OrderService orderService = new JdkInvocationHandler(new OrderServiceImpl()).getProxy();
//        orderService.order();

        // 使用cglib动态代理
//        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\code");
//        CglibMethodInterceptor cglibMethodInterceptor = new CglibMethodInterceptor();
//        Enhancer enhancer = new Enhancer();
//// 设置代理类的父类
//        enhancer.setSuperclass(OrderServiceImpl.class);
//// 设置回调对象
//        enhancer.setCallback(cglibMethodInterceptor);
//// 创建代理对象
//        OrderServiceImpl orderServiceImpl = (OrderServiceImpl) enhancer.create();
//        orderServiceImpl.order();


         OrderService orderService = new $Proxy01(new MyExtInvocationHandler(new OrderServiceImpl()));
         orderService.order();


    }

}
